package cn.js.sangu.unitStore.service.process;

import org.springframework.stereotype.Service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserComputerProcess {
    

    // public void ready(Device device){
    //     songguoyun(device.getAccount(), device.getPassword(), device.getDeviceName(), device.getComputer().toString());
    // }

    public boolean ready(String tAccount,String tPwd,String deviceNmae,Integer status){
        String value = "";
        //本地：开机1，关机2    远程：开机1，关机0，强制重启2，查询状态11，强制关机14
        if (status == 2) {
            value = "0";
        }else if (status == 1) {
            value = "1";
        }
        return songguoyunExecute(tAccount, tPwd, deviceNmae, value);
    }

    public Integer findStatus(String tAccount,String tPwd,String deviceNmae){
        String res = songguoyunFind(tAccount, tPwd, deviceNmae);
        if ("关机状态".equals(res)) {
            return 2;   
        }else if ("开机状态".equals(res)) {
            return 1;
        }
        return 0;
    }

    public String songguoyunFind(String account, String password, String deviceName){
        try {
            JSONObject json = new JSONObject();
            json.set("sgdz_account", account);
            json.set("sgdz_password", password);
            json.set("device_name", deviceName);
            json.set("value", "11");
            log.info("接口发送数据：{}", json.toString());
            String encodedJson = java.net.URLEncoder.encode(json.toString(), "UTF-8");
            HttpResponse response = HttpRequest.post("https://songguoyun.topwd.top/Esp_Api_new.php")
                .body(encodedJson)
                .execute();
            String responseBody = response.body();
            log.info("接口返回数据：{}", responseBody);
            JSONObject result = JSONUtil.parseObj(responseBody);
            if ("0".equals(result.getStr("status"))) {
                return "关机状态";
            } else if ("1".equals(result.getStr("status"))) {
                return "开机状态";
            } else {
                log.error("接口返回失败状态：{}", responseBody);
                return "查询失败";            
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "查询失败";
        }
    }

    public boolean songguoyunExecute(String account, String password, String deviceName, String value){
        try {
            JSONObject json = new JSONObject();
            json.set("sgdz_account", account);
            json.set("sgdz_password", password);
            json.set("device_name", deviceName);
            json.set("value", value);
            log.info("发送数据：{}", json.toString());
            String encodedJson = java.net.URLEncoder.encode(json.toString(), "UTF-8");
            HttpResponse response = HttpRequest.post("https://songguoyun.topwd.top/Esp_Api_new.php")
                .body(encodedJson)
                .execute();
            String responseBody = response.body();
            log.info("接口返回数据：{}", responseBody);
            JSONObject result = JSONUtil.parseObj(responseBody);
            if ("0".equals(result.getStr("status"))) {
                return true;
            } else {
                log.error("接口返回失败状态：{}", responseBody);
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
